using System;
using System.Configuration;

/// <summary>
/// Summary description for PaymentTransaction.
/// </summary>
public class AuthorizeNetPayment
{

	/// <summary>
	/// ham dung de thuc hien viec payment thong qua PaymentGateways.AuthorizeNet bang Credit Card
	/// </summary>
	/// <param name="CreditCardNumber"></param>
	/// <param name="ExpDate"></param>
	/// <param name="Amount"></param>
	/// <param name="Address"></param>
	/// <param name="City"></param>
	/// <param name="ZipCode"></param>
	/// <param name="State"></param>
	/// <returns>ket qua tra ve : 
	/// 0 372747 : thanh cong (ky tu dau tien la 0)  
	/// 1 dmhsgdgsmd : khong thanh cong
	/// </returns>
	public static string PaymentAuthorizeNetByCC(
		string CreditCardNumber,
		string ExpDate,
		decimal Amount,
		string Address,
		string City,
		string ZipCode,
		string State
		)
	{
		PaymentGateways.AuthorizeNet objAut = new PaymentGateways.AuthorizeNet();

		objAut.x_method = "CC";	
		try
		{
            objAut.x_login = System.Configuration.ConfigurationManager.AppSettings["AuthNetLogin"];
			objAut.x_tran_key = System.Configuration.ConfigurationManager.AppSettings["TransactionKey"];
			objAut.debug = bool.Parse(System.Configuration.ConfigurationManager.AppSettings["PaymentDebug"]);
			objAut.x_test_request = bool.Parse(System.Configuration.ConfigurationManager.AppSettings["PaymentTest"]);
			if (objAut.debug == true)
				objAut.AuthNetTransURL = System.Configuration.ConfigurationManager.AppSettings["AuthNetTransURL"];

			objAut.port = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["port"]);
			objAut.proxy = System.Configuration.ConfigurationManager.AppSettings["proxy"];
		}
		catch{}

		objAut.x_card_num = CreditCardNumber;
		objAut.x_exp_date = ExpDate;
		objAut.x_amount = Amount.ToString();
		objAut.x_address = Address;
		objAut.x_city = City;
		objAut.x_zip = ZipCode;
		objAut.x_state = State;

		objAut.Post();

		if (objAut.ErrorCode != 0)
		{
			return "1@" + objAut.ErrorString;
		}
		else
		{
			if (objAut.IsApproved == true)
				return "0@" + objAut.TransactionID;
			else
				return "1@" + "The transaction was declined for the following reason : " + objAut.ErrorPayment;
		}
	}

	/// <summary>
	/// ham dung de thuc hien viec payment thong qua PaymentGateways.AuthorizeNet bang Credit Card
	/// </summary>
	/// <param name="CreditCardNumber"></param>
	/// <param name="ExpDate"></param>
	/// <param name="Amount"></param>
	/// <param name="Address"></param>
	/// <param name="City"></param>
	/// <param name="ZipCode"></param>
	/// <param name="State"></param>
	/// <param name="FirstName"></param>
	/// <param name="LastName"></param>
	/// <returns>ket qua tra ve : 
	/// 0 372747 : thanh cong (ky tu dau tien la 0)  
	/// 1 dmhsgdgsmd : khong thanh cong
	/// </returns>
	public static string PaymentAuthorizeNetByCC(
		string CreditCardNumber,
		string ExpDate,
		decimal Amount,
		string Address,
		string City,
		string ZipCode,
		string State,
		string FirstName,
		string LastName
		)
	{
		PaymentGateways.AuthorizeNet objAut = new PaymentGateways.AuthorizeNet();

		objAut.x_method = "CC";			
		try
		{
			objAut.x_login = System.Configuration.ConfigurationManager.AppSettings["AuthNetLogin"];
			objAut.x_tran_key = System.Configuration.ConfigurationManager.AppSettings["TransactionKey"];
			objAut.x_test_request = bool.Parse(System.Configuration.ConfigurationManager.AppSettings["PaymentTest"]);
			objAut.debug = bool.Parse(System.Configuration.ConfigurationManager.AppSettings["PaymentDebug"]);
			if (objAut.debug == true)
				objAut.AuthNetTransURL = System.Configuration.ConfigurationManager.AppSettings["AuthNetTransURL"];

			objAut.port = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["port"]);
			objAut.proxy = System.Configuration.ConfigurationManager.AppSettings["proxy"];
		}
		catch{}

		objAut.x_card_num = CreditCardNumber;
		objAut.x_exp_date = ExpDate;
		objAut.x_amount = Amount.ToString();
		objAut.x_address = Address;
		objAut.x_city = City;
		objAut.x_zip = ZipCode;
		objAut.x_state = State;
		objAut.x_first_name = FirstName;
		objAut.x_last_name = LastName;

		objAut.Post();

		if (objAut.ErrorCode != 0)
		{
			return "1@" + objAut.ErrorString;
		}
		else
		{
			if (objAut.IsApproved == true)
				return "0@" + objAut.TransactionID;
			else
				return "1@" + "The transaction was declined for the following reason : " + objAut.ErrorPayment;
		}
	}

	/// <summary>
	/// ham dung de thuc hien viec payment thong qua PaymentGateways.AuthorizeNet bang Credit Card
	/// </summary>
	/// <param name="CreditCardNumber"></param>
	/// <param name="ExpDate"></param>
	/// <param name="Amount"></param>
	/// <param name="Address"></param>
	/// <param name="City"></param>
	/// <param name="ZipCode"></param>
	/// <param name="State"></param>
	/// <param name="CVV2">3 chu so cua the Credit card, dung de check security</param>
	/// <returns>ket qua tra ve : 
	/// 0 372747 : thanh cong (ky tu dau tien la 0)  
	/// 1 dmhsgdgsmd : khong thanh cong
	/// </returns>
	public static string PaymentAuthorizeNetByCC(
		string CreditCardNumber,
		string ExpDate,
		decimal Amount,
		string Address,
		string City,
		string ZipCode,
		string State,
		string CVV2
		)
	{
		PaymentGateways.AuthorizeNet objAut = new PaymentGateways.AuthorizeNet();

		objAut.x_method = "CC";			
		try
		{
			objAut.x_login = System.Configuration.ConfigurationManager.AppSettings["AuthNetLogin"];
			objAut.x_tran_key = System.Configuration.ConfigurationManager.AppSettings["TransactionKey"];
			objAut.x_test_request = bool.Parse(System.Configuration.ConfigurationManager.AppSettings["PaymentTest"]);
			objAut.debug = bool.Parse(System.Configuration.ConfigurationManager.AppSettings["PaymentDebug"]);
			if (objAut.debug == true)
				objAut.AuthNetTransURL = System.Configuration.ConfigurationManager.AppSettings["AuthNetTransURL"];
			objAut.port = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["port"]);
			objAut.proxy = System.Configuration.ConfigurationManager.AppSettings["proxy"];
		}
		catch{}

		objAut.x_card_num = CreditCardNumber;
		objAut.x_exp_date = ExpDate;
		objAut.x_amount = Amount.ToString();
		objAut.x_address = Address;
		objAut.x_city = City;
		objAut.x_zip = ZipCode;
		objAut.x_state = State;
		objAut.x_card_code = CVV2;

		objAut.Post();

		if (objAut.ErrorCode != 0)
		{
			return "1@" + objAut.ErrorString;
		}
		else
		{
			if (objAut.IsApproved == true)
				return "0@" + objAut.TransactionID;
			else
				return "1@" + "The transaction was declined for the following reason : " + objAut.ErrorPayment;
		}
	}

	/// <summary>
	/// ham dung de thuc hien viec payment thong qua PaymentGateways.AuthorizeNet bang Credit Card
	/// </summary>
	/// <param name="CreditCardNumber"></param>
	/// <param name="ExpDate"></param>
	/// <param name="Amount"></param>
	/// <param name="Address"></param>
	/// <param name="City"></param>
	/// <param name="ZipCode"></param>
	/// <param name="State"></param>
	/// <param name="CVV2">3 chu so cua the Credit card, dung de check security</param>
	/// <param name="FirstName"></param>
	/// <param name="LastName"></param>
	/// <returns>ket qua tra ve : 
	/// 0 372747 : thanh cong (ky tu dau tien la 0)  
	/// 1 dmhsgdgsmd : khong thanh cong
	/// </returns>
	public static string PaymentAuthorizeNetByCC(
		string CreditCardNumber,
		string ExpDate,
		decimal Amount,
		string Address,
		string City,
		string ZipCode,
		string State,
		string CVV2,
		string FirstName,
		string LastName
		)
	{
		PaymentGateways.AuthorizeNet objAut = new PaymentGateways.AuthorizeNet();

		objAut.x_method = "CC";			
		try
		{
			objAut.x_login = System.Configuration.ConfigurationManager.AppSettings["AuthNetLogin"];
			objAut.x_tran_key = System.Configuration.ConfigurationManager.AppSettings["TransactionKey"];
			objAut.x_test_request = bool.Parse(System.Configuration.ConfigurationManager.AppSettings["PaymentTest"]);
			objAut.debug = bool.Parse(System.Configuration.ConfigurationManager.AppSettings["PaymentDebug"]);
			if (objAut.debug == true)
				objAut.AuthNetTransURL = System.Configuration.ConfigurationManager.AppSettings["AuthNetTransURL"];
			objAut.port = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["port"]);
			objAut.proxy = System.Configuration.ConfigurationManager.AppSettings["proxy"];
		}
		catch{}

		objAut.x_card_num = CreditCardNumber;
		objAut.x_exp_date = ExpDate;
		objAut.x_amount = Amount.ToString();
		objAut.x_address = Address;
		objAut.x_city = City;
		objAut.x_zip = ZipCode;
		objAut.x_state = State;
		objAut.x_card_code = CVV2;
		objAut.x_first_name = FirstName;
		objAut.x_last_name = LastName;

		objAut.Post();

		if (objAut.ErrorCode != 0)
		{
			return "1@" + objAut.ErrorString;
		}
		else
		{
			if (objAut.IsApproved == true)
				return "0@" + objAut.TransactionID;
			else
				return "1@" + "The transaction was declined for the following reason : " + objAut.ErrorPayment;
		}
	}

	/// <summary>
	/// ham dung de thuc hien viec payment thong qua PaymentGateways.AuthorizeNet bang Bank Account
	/// </summary>
	/// <param name="BankName"></param>
	/// <param name="BankAccount"></param>
	/// <param name="BankABACode"></param>
	/// <param name="Address"></param>
	/// <param name="Amount"></param>
	/// <returns>ket qua tra ve : 
	/// 0 372747 : thanh cong (ky tu dau tien la 0)
	/// 1 dmhsgdgsmd : khong thanh cong
	/// </returns>
	public static string PaymentAuthorizeNetByBankAccount(
		string BankName,
		string BankAccount,
		string BankABACode,
		string Address,
		decimal Amount
		)
	{
		PaymentGateways.AuthorizeNet objAut = new PaymentGateways.AuthorizeNet();

		objAut.x_method = "ECHECK";
		try
		{
			objAut.x_login = System.Configuration.ConfigurationManager.AppSettings["AuthNetLogin"];
			objAut.x_tran_key = System.Configuration.ConfigurationManager.AppSettings["TransactionKey"];
			objAut.x_test_request = bool.Parse(System.Configuration.ConfigurationManager.AppSettings["PaymentTest"]);
			objAut.debug = bool.Parse(System.Configuration.ConfigurationManager.AppSettings["PaymentDebug"]);
			if (objAut.debug == true)
				objAut.AuthNetTransURL = System.Configuration.ConfigurationManager.AppSettings["AuthNetTransURL"];
			objAut.port = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["port"]);
			objAut.proxy = System.Configuration.ConfigurationManager.AppSettings["proxy"];
		}
		catch{}
		objAut.x_amount = Amount.ToString();
		objAut.x_bank_name = BankName;
		objAut.x_bank_acct_num = BankAccount;
		objAut.x_bank_aba_code = BankABACode;
		objAut.x_address = Address;

		objAut.Post();

		if (objAut.ErrorCode != 0)
		{
			return "1@" + objAut.ErrorString;
		}
		else
		{
			if (objAut.IsApproved == true)
				return "0@" + objAut.TransactionID;
			else
				return "1@" + "The transaction was declined for the following reason : " + objAut.ErrorPayment;
		}
	}

	/// <summary>
	/// thuc hien viec payment qua PaymentGateways.AuthorizeNet
	/// </summary>
	/// <param name="pData">cac thong tin ve payment</param>
	/// <param name="ByCC">true : payment By CC; false : payment By Check</param>
	/// <returns>
	/// ket qua tra ve : 
	/// 0 372747 : thanh cong (ky tu dau tien la 0)
	/// 1 dmhsgdgsmd : khong thanh cong
	/// </returns>	
	public static string PaymentAuthorizeNet(AuthorizeNetPaymentData pData, bool ByCC)
	{
		PaymentGateways.AuthorizeNet objAut = new PaymentGateways.AuthorizeNet();
				
		try
		{
			objAut.x_login = System.Configuration.ConfigurationManager.AppSettings["AuthNetLogin"];
			objAut.x_tran_key = System.Configuration.ConfigurationManager.AppSettings["TransactionKey"];
			objAut.debug = bool.Parse(System.Configuration.ConfigurationManager.AppSettings["PaymentDebug"]);
			objAut.x_test_request = bool.Parse(System.Configuration.ConfigurationManager.AppSettings["PaymentTest"]);

			if (objAut.debug == true)
				objAut.AuthNetTransURL = System.Configuration.ConfigurationManager.AppSettings["AuthNetTransURL"];

			objAut.port = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["port"]);
			objAut.proxy = System.Configuration.ConfigurationManager.AppSettings["proxy"];
		}
		catch{}

		// Customer Name and Billing Address
		objAut.x_first_name = pData.x_first_name;
		objAut.x_last_name = pData.x_last_name;
		objAut.x_company = pData.x_company;
		objAut.x_address = pData.x_address;
		objAut.x_city = pData.x_city;
		objAut.x_state = pData.x_state;
		objAut.x_zip = pData.x_zip;
		objAut.x_country = pData.x_country;
		objAut.x_phone = pData.x_phone;
		objAut.x_fax = pData.x_fax;
		objAut.x_cust_id = pData.x_cust_id;
		objAut.x_customer_ip = pData.x_customer_ip;
		objAut.x_customer_tax_id = pData.x_customer_tax_id;

		//Email Settings
		objAut.x_email = pData.x_email;
		objAut.x_email_customer = pData.x_email_customer;
		objAut.x_merchant_email = pData.x_merchant_email;
		objAut.x_header_email_receipt = pData.x_header_email_receipt;
		objAut.x_footer_email_receipt = pData.x_footer_email_receipt;

		//Invoice Information
		objAut.x_invoice_num = pData.x_invoice_num;
		objAut.x_description = pData.x_description;

		//Customer Shipping Address
		objAut.x_ship_to_first_name = pData.x_ship_to_first_name;
		objAut.x_ship_to_last_name = pData.x_ship_to_last_name;
		objAut.x_ship_to_company = pData.x_ship_to_company;
		objAut.x_ship_to_address = pData.x_ship_to_address;
		objAut.x_ship_to_city = pData.x_ship_to_city;
		objAut.x_ship_to_state = pData.x_ship_to_state;
		objAut.x_ship_to_zip = pData.x_ship_to_zip;
		objAut.x_ship_to_country = pData.x_ship_to_country;

		//Transaction Data
		objAut.x_amount = pData.x_amount;
		objAut.x_currency_code = pData.x_currency_code;
		if (ByCC == true)
		{
			objAut.x_method = "CC";
		}
		else
		{
			objAut.x_method = "ECHECK";
		}
		objAut.x_type = pData.x_type;
		objAut.x_recurring_billing = pData.x_recurring_billing;
			
		// - Credit Card
		objAut.x_card_num = pData.x_card_num;
		objAut.x_exp_date = pData.x_exp_date;
		objAut.x_card_code = pData.x_card_code;

		// - Check
		objAut.x_bank_aba_code = pData.x_bank_aba_code;
		objAut.x_bank_acct_num = pData.x_bank_acct_num;
		objAut.x_bank_acct_type = pData.x_bank_acct_type;
		objAut.x_bank_name = pData.x_bank_name;
		objAut.x_bank_acct_name = pData.x_bank_acct_name;

		//Level 2 Data
		objAut.x_po_num = pData.x_po_num;
		objAut.x_tax = pData.x_tax;
		objAut.x_tax_exempt = pData.x_tax_exempt;
		objAut.x_freight = pData.x_freight;
		objAut.x_duty = pData.x_duty;

		//Use Wells Fargo SecureSource Merchants			
		// - Additional Customer Data
		objAut.x_customer_organization_type = pData.x_customer_organization_type;
		objAut.x_drivers_license_num = pData.x_drivers_license_num;
		objAut.x_drivers_license_state = pData.x_drivers_license_state;
		objAut.x_drivers_license_dob = pData.x_drivers_license_dob;

		objAut.Post();

		if (objAut.ErrorCode != 0)
		{
			return "1@" + objAut.ErrorString;
		}
		else
		{
			if (objAut.IsApproved == true)
				return "0@" + objAut.TransactionID;
			else
				return "1@" + "The transaction was declined for the following reason : " + objAut.ErrorPayment;
		}
	}

	/// <summary>
	/// Ham dung de lay gia tri bao loi or Auth Transaction ID sau khi goi ham PaymentAuthorizeNetByCC or PaymentAuthorizeNetByBankAccount
	/// </summary>
	/// <param name="s"></param>
	/// <returns></returns>
	public static string GetValue(object s)
	{
		if (s == null)
			return string.Empty;
		string tmp = s.ToString();
		return tmp.Substring(tmp.LastIndexOf("@") + 1, tmp.Length - tmp.LastIndexOf("@") - 1).Trim();
	}
}
